﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.ViewModel;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class LogController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        BLL.Log Bll = new BLL.Log();
        /// <summary>
        /// 添加日志
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Log/Add")]
        public IHttpActionResult Add(Model.Log model)
        {
            log.Info($"LogType:{model.LogType};LogContent:{model.LogContent};LogAddTime:{model.LogAddTime}");

            ResultMsg resultMsg = new ResultMsg();

            Model.Log Mol = new Model.Log();
            Mol.LogType = model.LogType;
            Mol.LogContent = model.LogContent;
            Mol.LogAddTime = DateTime.Now;

            int flag = Bll.Add(Mol);
            if (flag > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = flag;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = "添加失败";
                resultMsg.Data = flag;
            }


            return Json(resultMsg);
        }
        /// <summary>
        /// 得到日志列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Log/Sel")]
        public IHttpActionResult Sel()
        {
            log.Info($"获取日志列表");
            ResultMsg resultMsg = new ResultMsg();
            DataTable dt = Bll.GetAllList().Tables[0];
            if (dt.Rows.Count > 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = dt;
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "没有获取到日志信息";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/Log/Del")]
        public IHttpActionResult Del(Model.Log model)
        {
            log.Info($"删除日志");
            ResultMsg resultMsg = new ResultMsg();

            if (Bll.Delete(model.LID))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "删除信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }
      
     
    }
}
